package com.fundcrawler.entity;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 东方财富概念板块实体
 * 对应 API: https://push2.eastmoney.com/api/qt/clist/get
 */
@Data
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "concept_board", indexes = {
    @Index(name = "idx_code", columnList = "code"),
    @Index(name = "idx_name", columnList = "name"),
    @Index(name = "idx_change_percent", columnList = "change_percent")
})
public class ConceptBoard {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * f1: 当前排名
     */
    @JsonProperty("f1")
    @Column(name = "rank")
    private Integer rank;

    /**
     * f2: 成交量（单位：手）
     */
    @JsonProperty("f2")
    @Column(name = "total_volume")
    private Long totalVolume;

    /**
     * f3: 涨幅（百分比，如 2.31）
     */
    @JsonProperty("f3")
    @Column(name = "change_percent", precision = 5, scale = 2)
    private Double changePercent;

    /**
     * f4: 下跌家数
     */
    @JsonProperty("f4")
    @Column(name = "falling_stocks")
    private Integer fallingStocks;

    /**
     * f8: 净涨家数（上涨 - 下跌）
     */
    @JsonProperty("f8")
    @Column(name = "net_change_count")
    private Integer netChangeCount;

    /**
     * f12: 板块代码，如 BK0638
     */
    @JsonProperty("f12")
    @Column(name = "code", length = 20, nullable = false)
    private String code;

    /**
     * f13: 市场类型（90=概念板块）
     */
    @JsonProperty("f13")
    @Column(name = "market")
    private Integer market;

    /**
     * f14: 板块名称，如“华为概念”
     */
    @JsonProperty("f14")
    @Column(name = "name", length = 100, nullable = false)
    private String name;

    /**
     * f152: 数据状态码
     */
    @JsonProperty("f152")
    @Column(name = "data_status")
    private Integer dataStatus;

    /**
     * f20: 总市值（单位：元）
     */
    @JsonProperty("f20")
    @Column(name = "total_value", precision = 18, scale = 2)
    private BigDecimal totalValue;

    /**
     * f104: 换手率（千分比，如 98 = 9.8%）
     */
    @JsonProperty("f104")
    @Column(name = "turnover_rate_permille")
    private Integer turnoverRatePermille;

    /**
     * f105: 涨停家数或涨跌停统计
     */
    @JsonProperty("f105")
    @Column(name = "limit_up_count")
    private Integer limitUpCount;

    /**
     * f128: 领涨股名称
     */
    @JsonProperty("f128")
    @Column(name = "leading_stock_name", length = 50)
    private String leadingStockName;

    /**
     * f140: 领涨股代码
     */
    @JsonProperty("f140")
    @Column(name = "leading_stock_code", length = 20)
    private String leadingStockCode;

    /**
     * f141: 领涨股涨幅（单位：百分之一，如 200 = 2.00%）
     */
    @JsonProperty("f141")
    @Column(name = "leading_stock_change_percent")
    private Integer leadingStockChangePercent;

    /**
     * f207: 第二领涨股名称
     */
    @JsonProperty("f207")
    @Column(name = "second_leading_stock_name", length = 50)
    private String secondLeadingStockName;

    /**
     * f208: 第二领涨股代码
     */
    @JsonProperty("f208")
    @Column(name = "second_leading_stock_code", length = 20)
    private String secondLeadingStockCode;

    /**
     * f209: 第二领涨股涨幅（百分之一）
     */
    @JsonProperty("f209")
    @Column(name = "second_leading_stock_change_percent")
    private Integer secondLeadingStockChangePercent;

    /**
     * f136: 板块内股票总数
     */
    @JsonProperty("f136")
    @Column(name = "total_stocks")
    private Integer totalStocks;

    /**
     * f222: 资金净流入（单位：万元，负值为净流出）
     */
    @JsonProperty("f222")
    @Column(name = "net_inflow")
    private Integer netInflow;

    /**
     * 数据入库时间
     */
    @Column(name = "created_time")
    private LocalDateTime createdTime;

    @PrePersist
    protected void onCreate() {
        this.createdTime = LocalDateTime.now();
    }
}
